﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests
{
    [TestClass]
    public class FulcrumSettingsTests
    {
        public FulcrumSettingsTests()
        {
            // nothing
        }

        FulcrumSettings settings;

        [TestInitialize]
        public void TestInitialize()
        {
            this.settings = new FulcrumSettings();
        }

        [TestMethod]
        public void TestModule()
        {
            this.settings.Module = "Foo";

            Assert.AreEqual("Foo", this.settings.Module);
        }

        [TestMethod]
        public void TestModuleChanged()
        {
            var watcher = new EventHandlerWatcher();

            this.settings.ModuleChanged += watcher.OnEvent;

            this.settings.Module = "Bar";

            Assert.AreSame(this.settings, watcher.Sender);
            Assert.IsNotNull(watcher.EventArgs);
            Assert.AreEqual(typeof(EventArgs), watcher.EventArgs.GetType());
        }

        class EventHandlerWatcher
        {
            bool eventRaised = false;

            public EventHandlerWatcher()
            {
                // nothing
            }

            public void OnEvent(object sender, EventArgs e)
            {
                Assert.IsFalse(this.eventRaised);
                this.eventRaised = true;

                this.Sender = sender;
                this.EventArgs = e;
            }

            public object Sender
            {
                get;
                private set;
            }

            public EventArgs EventArgs
            {
                get;
                private set;
            }
        }

        [TestMethod]
        public void TestValidateSettings()
        {
            this.settings.Module = "Foo";
            this.settings.OutputDirectory = "Output";

            this.settings.ValidateSettings();

            //
            // No exception is pass
            //
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void TestValidateSettingsNoOutputPath()
        {
            this.settings.Module = "Foo";

            this.settings.ValidateSettings();
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void TestValidateSettingsNoModule()
        {
            this.settings.OutputDirectory = "Output";

            this.settings.ValidateSettings();
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void TestValidateSettingsEmptyOutputPath()
        {
            this.settings.Module = "Foo";
            this.settings.OutputDirectory = String.Empty;

            this.settings.ValidateSettings();
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void TestValidateSettingsEmptyModule()
        {
            this.settings.Module = String.Empty;
            this.settings.OutputDirectory = "Output";

            this.settings.ValidateSettings();
        }

    }
}
